package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.stereotype.Component;

import dao.CustomerDao;
import pojo.Customer;

@Component("customerDao")
public class CustomerDaoImpl implements CustomerDao{

	@Resource(name="dataSource1")
	DataSource ss;
	
	@Override
	public int addCustomer(Customer c) {
		Connection con;
		PreparedStatement ps;
		try {
		 con = ss.getConnection();
		c.setId(UUID.randomUUID().toString());
		String sql = "insert into customers(id,cname,sex, sal,birth) values(?,?,?,?,?)";
		
			ps = con.prepareStatement(sql);
		
		ps.setString(1, c.getId());
		ps.setString(2, c.getCname());
		ps.setBoolean(3, c.isSex());
		ps.setDouble(4, c.getSal());
		if(c.getBirth()==null) {
			ps.setDate(5, null);	
		}else {
			ps.setDate(5, new java.sql.Date(c.getBirth().getTime()));
		}
		
		
		int m = ps.executeUpdate();
		ps.close();
		con.close();
		return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public int updateCustomer(Customer c) {
		Connection con;
		PreparedStatement ps;
		try {
		 con = ss.getConnection();
		String sql = "update customers set cname=?,sex=?, sal=?,birth=? where id=?";
		ps = con.prepareStatement(sql);
		
		
		ps.setString(1, c.getCname());
		ps.setBoolean(2, c.isSex());
		ps.setDouble(3, c.getSal());
		if(c.getBirth()==null) {
			ps.setDate(4, null);	
		}else {
			ps.setDate(4, new java.sql.Date(c.getBirth().getTime()));
		}
		ps.setString(5, c.getId());
		
		int m = ps.executeUpdate();
		ps.close();
		con.close();
		return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public List<Customer> findAllCustomers() {
		Connection con;
		PreparedStatement ps;
		List<Customer> list = new ArrayList<Customer>();
		try {
		 con = ss.getConnection();
		String sql = "select * from customers";
		ps = con.prepareStatement(sql);
		
		ResultSet set = ps.executeQuery();
		while(set.next()) {
			Customer c = new Customer();
			c.setId(set.getString("id"));
			c.setCname(set.getString("cname"));
			c.setSal(set.getDouble("sal"));
			java.sql.Date dd = set.getDate("birth");
			if(dd==null) {
				c.setBirth(null);
			}else {
				c.setBirth(new Date(dd.getTime()));
			}
			c.setSex(set.getBoolean("sex"));
			list.add(c);
		}
		ps.close();
		con.close();
		return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int deleteCustomer(String id) {
		Connection con;
		PreparedStatement ps;
		try {
		 con = ss.getConnection();
		String sql = "delete customers where id = ?";
		
			ps = con.prepareStatement(sql);
		
		ps.setString(1, id);
	
		int m = ps.executeUpdate();
		ps.close();
		con.close();
		return m;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

}
